package com.ptszyxx.popose.module.chat.page;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.constants.BundleConstant;
import com.ptszyxx.popose.databinding.FragmentChatSetBinding;
import com.ptszyxx.popose.module.chat.page.vm.ChatSetVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.dialog.ConfirmDialog;

/**
 * 聊天设置
 */
public class ChatSetFragment extends BaseFragment<FragmentChatSetBinding, ChatSetVM> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_chat_set;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public ChatSetVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(ChatSetVM.class);
    }

    @Override
    public void initParam() {
        super.initParam();
        Bundle bundle = getArguments();
        if (bundle != null){
            viewModel.txUserId = bundle.getString(BundleConstant.CHAT_USER_ID);
            viewModel.userName = bundle.getString(BundleConstant.CHAT_USER_NAME);
            viewModel.userPic = bundle.getString(BundleConstant.CHAT_USER_PIC);
        }
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestUser();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onClearChatEvent.observe(this, o -> showDialogClearChat());
        viewModel.uc.onBlackEvent.observe(this, o -> showDialogBlack());
        viewModel.uc.onFollowEvent.observe(this, o -> setFollowUI());
    }

    /**
     * 关注UI
     */
    private void setFollowUI() {
        if (YStringUtil.eq(1, viewModel.userEntity.getGuanzhuState())) {
            binding.tvFollow.setText(getResources().getString(R.string.follow_cancel));
            binding.tvFollow.setTextColor(getResources().getColor(com.ysg.R.color.pinkColor));
            binding.tvFollow.setBackgroundResource(com.ysg.R.drawable.shape_white_oval);
        } else {
            binding.tvFollow.setText(getResources().getString(R.string.follow));
            binding.tvFollow.setTextColor(getResources().getColor(com.ysg.R.color.white));
            binding.tvFollow.setBackgroundResource(com.ysg.R.drawable.shape_pink_oval);
        }
    }

    /**
     * 清空聊天
     */
    private void showDialogClearChat() {
        YDialogUtil.getInstance().showConfirm(getContext(), getResources().getString(R.string.chat_clear_message_tip))
                .setOnConfirmListener(new ConfirmDialog.OnConfirmListener() {
                    @Override
                    public void onConfirmClick() {
                        viewModel.clearChat();
                    }
                });
    }

    /**
     * 黑名单
     */
    private void showDialogBlack() {
        YDialogUtil.getInstance().showConfirm(getContext(), getResources().getString(R.string.black_dialog_title), getResources().getString(R.string.black_dialog_content))
                .setOnConfirmListener(new ConfirmDialog.OnConfirmListener() {
                    @Override
                    public void onConfirmClick() {
                        viewModel.requestBlack();
                    }
                });
    }
}
